﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
     class AutoChessMapMgr
    {
        #region ins
        private static AutoChessMapMgr ins;
        public static AutoChessMapMgr Ins
        {
            get
            {
                if (ins == null)
                {
                    ins = new AutoChessMapMgr();
                }
                return ins;
            }
        }
        #endregion

        private List<ACGrid> formationGrids = new List<ACGrid>();
        private List<ACGrid> preChooseGrids = new List<ACGrid>();
        private List<ACGrid> enemy_Grids = new List<ACGrid>();
        private List<ACGrid> center_Grids = new List<ACGrid>();

        public List<ACGrid> shopGrids = new List<ACGrid>();

        private ACGrid curSelectGrid;
        private ACGrid curHighLightGrid;
        public ACGrid CurSelectGrid
        {
            get { return curSelectGrid; }
        }

        public void InitMap()
        {
            ExLog.LogWarning("初始化格子~~~");

            shopGrids.Clear();
            formationGrids.Clear();
            preChooseGrids.Clear();
            enemy_Grids.Clear();
            center_Grids.Clear();

            var shopDatas = AutoChessFieldViewCtr.Ins.SceneConfig.GetShopGridConfig();
            var fightDatas = AutoChessFieldViewCtr.Ins.SceneConfig.GetFightGridConfig();
            var enemyDatas = AutoChessFieldViewCtr.Ins.SceneConfig.GetEnemyGridConfig();
            var preChossDatas = AutoChessFieldViewCtr.Ins.SceneConfig.GetPreChooseGridConfig();
            var centerDatas = AutoChessFieldViewCtr.Ins.SceneConfig.GetCenterGridConfig();
            foreach (var grid in shopDatas)
            {
                var gridUnit = ActorManager.Instance.CreateACGrid(grid);
                shopGrids.Add(gridUnit);
            }
            foreach (var grid in fightDatas)
            {
                var gridUnit = ActorManager.Instance.CreateACGrid(grid);
                formationGrids.Add(gridUnit);
            }
            foreach (var grid in preChossDatas)
            {
                var gridUnit = ActorManager.Instance.CreateACGrid(grid);
                preChooseGrids.Add(gridUnit);
            }
            foreach (var grid in enemyDatas)
            {
                var gridUnit = ActorManager.Instance.CreateACGrid(grid);
                enemy_Grids.Add(gridUnit);
            }
            foreach (var grid in centerDatas)
            {
                var gridUnit = ActorManager.Instance.CreateACGrid(grid);
                center_Grids.Add(gridUnit);
            }
        }
        
        public void Release()
        {
            curSelectGrid = null;
            curHighLightGrid = null;
            
            formationGrids.Clear();
            preChooseGrids.Clear();
            shopGrids.Clear();
            enemy_Grids.Clear();
            center_Grids.Clear();

        }

        /// <summary>
        /// 获取对应的区域的格子
        /// </summary>
        /// <param name="type"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public ACGrid GetGridPos(GridType type ,int pos)
        {
            if (type == GridType.FightGrid)
            {
                return formationGrids[pos];
            }
            else if (type == GridType.PreChooseGrid)
            {
                return preChooseGrids[pos];
            }
            else if (type == GridType.ShopGrid)
            {
                return shopGrids[pos];
            }
            else if (type == GridType.EnemyGrid)
            {
                return enemy_Grids[pos];
            }
            else if (type == GridType.CenterGrid)
            {
                return center_Grids[pos];
            }
            ExLog.LogError("构建实体致命错误");
            return null;
        }

        #region OpretionAction

        /// <summary>
        /// 格子操作
        /// </summary>
        /// <param name="msg"></param>gridUnitRender
        public void RunAction(GridOpretionMsg msg)
        {
            if (msg is SelectGridMsg)
            {
                var data = msg as SelectGridMsg;
                if(curSelectGrid != null)
                {
                    curSelectGrid.IsSelect = false;
                    AutoChessManager.Ins.SetFloorEffectSelect(false);
                    EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, null);
                }
                //会不会 有一闪的问题？ 如果action 就是select ，那就是先自己隐藏然后又显示
                if(data.action != null)
                {
                    curSelectGrid = data.action;
                    curSelectGrid.IsSelect = true;
                    EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, curSelectGrid);
                    if(CurSelectGrid.GridType != GridType.ShopGrid)
                        AutoChessManager.Ins.SetFloorEffectSelect(true, curSelectGrid);
                }

                if(curSelectGrid == null && data.action == null)
                {
                    EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, null);
                }
            }
            else if (msg is HighLightGridMsg)
            {
                var data = msg as HighLightGridMsg;
                if (curHighLightGrid != null)
                {
                    AutoChessManager.Ins.SetFloorEffectHighLight(false);
                }
                if (data.action != null)
                {
                    curHighLightGrid = data.action;
                    AutoChessManager.Ins.SetFloorEffectHighLight(true, curHighLightGrid);
                }
            }
            else if (msg is SwitchGridHeroMsg) //等服务器消息来 创建交换指令
            {
                var data = msg as SwitchGridHeroMsg;
                var fromGrid = data.action;
                var toGrid = data.targetGrid;
                var from = fromGrid.heroUnit;
                var to = toGrid.heroUnit;
                //from.EnterGrid(toGrid);
                ActorEventHelper.SendHeroEnterGrid(from,toGrid);
                fromGrid.IsSelect = false;
                if (to != null)
                {
                    //to.EnterGrid(fromGrid);
                    ActorEventHelper.SendHeroEnterGrid(to,fromGrid);
                    toGrid.IsSelect = false;
                }
                if(toGrid.GridType == GridType.FightGrid && fromGrid.GridType != GridType.FightGrid)
                {
                    AutoChessManager.Ins.PlayHeroSetSound(from.HeroID);
                }
                AutoChessManager.Ins.SetFloorEffectSelect(false);
                EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, null);
                EventManager.TriggerEvent<GridType>(CommonEvent.ON_GAMEFIELD_CHESS_UPDATE, toGrid.GridType);
            }
        }

        /// <summary>
        /// 英雄操作
        /// </summary>
        /// <param name="msg"></param>
        public void RunAction(HeroOpretionMsg msg)
        {
           if(msg is HeroDisappearMsg)
            {
                var data = msg as HeroDisappearMsg;
                var removePackage = data.package;
                var gridType = removePackage.gridType;
                var curGrid = GetGridPos(gridType, removePackage.gridIndex);
                if (curGrid.heroUnit != null)
                {
                    //curGrid.heroUnit.LeaveBattleField();
                    AutoChessPlayerCtr.Ins.DeleteHeroUnit(curGrid.heroUnit);
                    Yunchang.EventManager.TriggerEvent<GridType>(CommonEvent.ON_GAMEFIELD_CHESS_UPDATE, gridType);
                    switch (data.opType)
                    {
                        case ProtoBuf.Message.AutoChessOpType.AUTO_CHESS_OP_SELL:
                            {
                                AutoChessManager.Ins.ShotSellChessEffect(curGrid);
                                EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, null);
                                AutoChessManager.Ins.SetFloorEffectSelect(false);
                            }
                            break;
                    }
                }
                else
                {
                    AutoChessManager.DebugLog("当前逻辑指令没有寻找到正确的删除逻辑体!");
                }
            }
           else if(msg is HeroAppearMsg)
            {
                var data = msg as HeroAppearMsg;
                var heroData = data.heroData;
                var gridUnit = GetGridPos(heroData.gridType, heroData.gridIndex);
                if (gridUnit == null)
                {
                    ExLog.LogError(heroData.ToString()+"  grid null");
                    return;
                }
                if(gridUnit.heroUnit != null)
                {
                    AutoChessPlayerCtr.Ins.DeleteHeroUnit(gridUnit.heroUnit);
                    //gridUnit.heroUnit.LeaveBattleField();
                }
                AutoChessPlayerCtr.Ins.AddHeroUnit(heroData);
                Yunchang.EventManager.TriggerEvent<GridType>(CommonEvent.ON_GAMEFIELD_CHESS_UPDATE, gridUnit.GridType);
                // todo: 不同类型触发不同特效
                switch (data.opType)
                {
                    case ProtoBuf.Message.AutoChessOpType.AUTO_CHESS_OP_STAR_GROWTH:
                        AutoChessManager.Ins.ShotStarUpgradeEffect(gridUnit);
                        AutoChessManager.Ins.CurrentAutoChessField.AutoUpgradeStarMsg(gridUnit);
                        //gridUnit.heroUnit.ShowUp(null);
                        ActorEventHelper.SendActorStateChange(gridUnit.heroUnit, ActorStateEvent.Actor_Appearance);
                        EventManager.TriggerEvent<ACGrid>(CommonEvent.ON_AUTOCHESS_SELECT_UNIT, null);
                        AutoChessManager.Ins.SetFloorEffectSelect(false);
                        break;
                    case ProtoBuf.Message.AutoChessOpType.AUTO_CHESS_OP_BUG:
                        AutoChessManager.Ins.ShotAppearEffect(gridUnit);
                        AutoChessManager.Ins.CurrentAutoChessField.AutoUpgradeStarMsg(gridUnit);
                        //gridUnit.heroUnit.ShowUp(null);
                        ActorEventHelper.SendActorStateChange(gridUnit.heroUnit, ActorStateEvent.Actor_Appearance);
                        break;
                    case ProtoBuf.Message.AutoChessOpType.AUTO_CHESS_OP_REFRESH_BUG_CHESSES:
                        //gridUnit.heroUnit.ShowUp(null);
                        ActorEventHelper.SendActorStateChange(gridUnit.heroUnit, ActorStateEvent.Actor_Appearance);
                        break;
                }
                if(gridUnit.GridType == GridType.FightGrid || data.opType == ProtoBuf.Message.AutoChessOpType.AUTO_CHESS_OP_BUG)
                {
                    var tableChess = Table.Blo.AutoChessBlo.GetChessTable(heroData.chessId);
                    AutoChessManager.Ins.PlayHeroSetSound(tableChess.heroId);
                }
            }
        }

        public List<ACHero> GetShopHeros()
        {
            List<ACHero> result = new List<ACHero>();
            for (int i = 0; i < shopGrids.Count; i++)
            {
                result.Add(shopGrids[i].heroUnit);//可能为空
            }
            return result;
        }


        public bool IsSelectGrid(ACGrid grid)
        {
            if (curSelectGrid == null)
            {
                return false;
            }
            else
            {
                return curSelectGrid == grid;
            }
        }
   
        #endregion

    }
}
